package com.buta.common.action;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.interceptor.PrincipalProxy;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;


public class BaseAction extends ActionSupport implements ServletRequestAware,PrincipalAware,ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6131032065662591128L;
	protected HttpServletRequest httpServletRequest;
	protected HttpServletResponse httpServletResponse;
	// ===== Principal =========================
	protected PrincipalProxy principalProxy;
	
	@Override
	public void setServletResponse(HttpServletResponse httpServletResponse) {
		this.httpServletResponse = httpServletResponse;		
	}
	@Override
	public void setPrincipalProxy(PrincipalProxy principalProxy) {
		this.principalProxy = principalProxy;		
	}
	@Override
	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.httpServletRequest = httpServletRequest;
		
	}
	
	
	public String start() throws Exception {
		return ActionSupport.SUCCESS;
	}
	
	public String end() throws Exception {
		clearSessionValue();
		return ActionSupport.SUCCESS;
	}
	
	public void clearSessionValue()
	{
		for (Enumeration e = httpServletRequest.getSession().getAttributeNames(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            if( key.indexOf(getClass().toString()) >= 0 ){
            	httpServletRequest.getSession().removeAttribute(key);
            }
        }
	}
	
	
	
}
